﻿using System;
using System.Collections.Generic;
using SystemTypes;
using Semantic;
using MindFusion.Diagramming;

namespace WindowsFormsApplication1
{
    class Converter
    {
        public static SemanticNetwork Diagram2SemanticNet(Diagram diagram)
        {
            SemanticNetwork sn = new SemanticNetwork();
            UnnamedNode newNode;
            ShapeNode node;
            for (int i = 0; i < diagram.Nodes.Count; i++)
            {
                node = diagram.Nodes[i] as ShapeNode;
                if (node.ToolTip == "named")
                {
                    newNode = new NamedNode(node.Text);
                    if (node.Tag != null)
                    {
                        (newNode as NamedNode).Synset = node.Tag as List<string>;
                    }
                }
                else
                    newNode = new UnnamedNode();
                if (node.Id != null)
                    newNode.ID = (Guid)(node.Id);
                else
                {
                    newNode.ID = Guid.NewGuid();
                    node.Id = newNode.ID;
                }
                newNode.OutArcs = new List<Guid>();
                newNode.InArcs = new List<Guid>();
                sn.AddNewNode(newNode);
                
            }
            int num;
            Guid g;
            sn.Arcs = new List<Arc>();
            DiagramLink link;
            for (int i = 0; i < diagram.Links.Count; i++)
            {
                link = diagram.Links[i];
                Arc arc = new Arc();
                switch (link.Text)
                {
                    case "is_a": arc.Type = (Types.ArcType.IsA); break;
                    case "a_part_of": arc.Type = (Types.ArcType.APartOf); break;
                    case "element_of": arc.Type = (Types.ArcType.ElementOf); break;
                    case "use": arc.Type = (Types.ArcType.Use); break;
                    case "precede": arc.Type = (Types.ArcType.Precede); break;
                    case "restriction": arc.Type = (Types.ArcType.Restriction); break;
                    case "name": arc.Type = (Types.ArcType.Name); break;
                }                
                if (link.Id != null)
                    arc.ID = (Guid)(link.Id);
                else
                {
                    arc.ID = Guid.NewGuid();
                    link.Id = arc.ID;
                }
                arc.OutNode = sn.Nodes[diagram.Nodes.IndexOf(link.Origin)].ID;
                arc.InNode = sn.Nodes[diagram.Nodes.IndexOf(link.Destination)].ID;
                sn.Nodes[diagram.Nodes.IndexOf(link.Origin)].OutArcs.Add(arc.ID);
                sn.Nodes[diagram.Nodes.IndexOf(link.Destination)].InArcs.Add(arc.ID);
                sn.Arcs.Add(arc);                
            }
            return sn;
        }
    }
}
